package cn.rengy.web.framework.sys.login.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.rengy.lang.ResultEntity;
import cn.rengy.tool.web._WebUtils;
import cn.rengy.web.framework.shiro.token.LocalToken;
import cn.rengy.web.framework.shiro.token.UsernameSecurityCodeToken;
import cn.rengy.web.framework.shiro.token.WechatToken;
import cn.rengy.web.framework.util.GetSessionUtils;


/**
 * 负责打开登录页面(GET请求)和登录出错页面(POST请求)，
 * 
 * 
 */
@Controller
@RequestMapping(value = "/")
public class LoginController {
	private static Logger logger = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	private SysLoginService sysLoginService;
	@RequestMapping(value="/authenticated/test/test",method=RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> test(@RequestBody Map<String,Object> paramMap){
		System.out.println(GetSessionUtils.getUserId());
		return ResultEntity.ok();
	}
	/**
	 * 刷新token
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="refresh_token",method=RequestMethod.POST)
	@ResponseBody
	public ResultEntity<?> refreshToken(@RequestBody Map<String,Object> paramMap){
		String refresh_token=MapUtils.getString(paramMap, "refresh_token");
		sysLoginService.refreshToken(refresh_token) ;
		return ResultEntity.ok();
	}
	/**
	 * 可以设置/访问页面，默认index.html
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(){
		return "/login";
	}
	@RequestMapping(value="loginSuccess",method = RequestMethod.GET)
	public String loginSuccess(HttpServletRequest request){
		return "/view/sys/main/main.html";
	}
	@RequestMapping(value="login",method = RequestMethod.GET)
	public String login(HttpServletRequest request){
		String userAgent=request.getHeader("User-Agent");
//		String referer=request.getHeader("Referer");
//		logger.debug(referer);
//		logger.debug(userAgent);
		if(userAgent.indexOf("MicroMessenger")!=-1){//微信端
			String redirect=request.getParameter("redirect");
			if(StringUtils.isNotBlank(redirect)){
				return "/wx/oauthRedirectV2?redirect="+_WebUtils.urlEncode(redirect);
			}else{
				return "/view/weixin/login.html";
			}
		}else{
			return "/view/login/login.html";
		}
	}
	/**
	 * 发送微信验证码
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="sendSecurityCode",method=RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> sendSecurityCode(@RequestBody Map<String,Object> paramMap){
		//
        return sysLoginService.sendSecurityCode(paramMap);
    }
	/**
	 * 微信验证码登录
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="securityCodeLogin",method=RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> securityCodeLogin(@RequestBody Map<String,Object> paramMap){
		String username=MapUtils.getString(paramMap, "username");
		String password=MapUtils.getString(paramMap, "password");
		UsernameSecurityCodeToken token = new UsernameSecurityCodeToken(username, password);
    	sysLoginService.login(token);
        return ResultEntity.ok(sysLoginService.getUserInfo());
    }
	/**
	 * 用户名密码登录
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="login",method=RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> login(HttpServletRequest request,@RequestBody Map<String,String> paramMap){
		String username=paramMap.get("username");
		String password=paramMap.get("password");
		
		LocalToken token = new LocalToken(username, password,_WebUtils.getUserAgent(request),_WebUtils.getClientIp(request));
    	sysLoginService.login(token);
        return ResultEntity.ok();
    }
	/**
	 * 微信登录
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="wxlogin",method=RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> wxlogin(@RequestBody Map<String,Object> paramMap){
		//String state=paramMap.get("state");
		String code=MapUtils.getString(paramMap,"code");
		String appid=MapUtils.getString(paramMap,"appid");
		WechatToken token = new WechatToken(code,appid);
    	sysLoginService.login(token);
    	
        return ResultEntity.ok(sysLoginService.getUserInfo());
    }
	@RequestMapping(value="logout",method=RequestMethod.GET)  
    public String logout(RedirectAttributes redirectAttributes ){
        //使用权限管理工具进行用户的退出，跳出登录，给出提示信息
        SecurityUtils.getSubject().logout();
        //redirectAttributes.addFlashAttribute("message", "您已安全退出!");  
        return "redirect:/login";
    } 
}
